﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgramMain.Database;
using ProgramMain.Database.SimpleOpticNetDBTableAdapters;

namespace ProgramMain.OpticNet
{
    public partial class SelectFibreAutoConnectCtl : ProgramMain.Framework.BaseModalDetailCtl
    {
        public SelectFibreAutoConnectCtl()
        {
            InitializeComponent();
        }

        protected override void InitializeDataControls()
        {
            int finish = 1;
            if (_cable1ID != null && _cable2ID != null)
            {
                if (_cable1ID.FibreCount < _cable2ID.FibreCount) 
                    finish= _cable1ID.FibreCount;
                else
                    finish= _cable2ID.FibreCount;

                txtCable1Finish.Text = finish.ToString();
                txtCable2Finish.Text = finish.ToString();
            }
        }

        private SimpleOpticNetDB.CableRow _cable1ID = null, _cable2ID = null;
        private void LoadCableByID(int? cableID, int CableSteck)
        {
            CableTableAdapter cableTableAdapter = new CableTableAdapter();
            cableTableAdapter.ClearBeforeFill = true;
            cableTableAdapter.SelectCommandTimeout = Properties.Settings.Default.LongQueryTimeout;
            SimpleOpticNetDB.CableDataTable cableDataTable = cableTableAdapter.GetData(null, null, cableID);

            if (CableSteck == 1 && cableDataTable.Count > 0)
            {
                _cable1ID = cableDataTable[0];
                layoutCable1.Text = string.Format("{0}  -  {1} волокн(а)", _cable1ID.Caption, _cable1ID.FibreCount);
                
            }
            if (CableSteck == 2 && cableDataTable.Count > 0)
            {
                _cable2ID = cableDataTable[0];
                layoutCable2.Text = string.Format("{0}  -  {1} волокн(а)", _cable2ID.Caption, _cable2ID.FibreCount);
            }
        }

        public int? Cable1ID
        {
            set { LoadCableByID(value, 1); }
        }

        public int? Cable2ID
        {
            set { LoadCableByID(value, 2); }
        }

        public int? FibreMaxCount
        {
            get
            {
                if(_cable1ID !=null && _cable2ID != null)
                {
                    if (_cable1ID.FibreCount > _cable2ID.FibreCount) return _cable1ID.FibreCount;
                   return _cable2ID.FibreCount;
                }
                return null;
            }
        }

        public int FibreCount { get; private set; }
        public int start1 = 0;
        public int start2 = 0;
        public int finish1 = 0;
        public int finish2 = 0;
        public override void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                bool flag = true;
               flag = flag && int.TryParse(txtCable1Start.Text, out start1);
               flag = flag && int.TryParse(txtCable2Start.Text, out start2);
               flag = flag && int.TryParse(txtCable1Finish.Text, out finish1);
               flag = flag && int.TryParse(txtCable2Finish.Text, out finish2);

                if (!flag) throw new Exception("Не верно задана емкость разварки");
                flag = ((finish1 - start1) == (finish2 - start2));
                if (!flag) throw new Exception("Не верно задана емкость разварки");
                FibreCount = finish2 - start2 + 1;
            }
            catch (Exception Error)
            {
                XtraMessageBox.Show(Error.Message, "Сообщение", MessageBoxButtons.OK);
                return;
            }
           
            if (this.ParentForm != null)
            {
                this.ParentForm.DialogResult = DialogResult.OK;     
                this.ParentForm.Close();
            }
        }
    }
}
